﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TERP.Constants;
using TERP.WaterClient.Common;
using CommonUtils;
using TERP.WaterClient.DbServices;

namespace TERP.WaterClient.Views.Water
{
    public partial class FrmCustomer : FrmMdiBase
    {
        #region Properties

		public delegate void SendValue(string _strCusID, string _strAddress, string _strCusName, string _strCusTel, string _strOrgName, string _strDebit);
		public SendValue _sendValue = null;

		//public ITS.Library.WaterCustomerService.WaterCustomer _customer;
        private bool isclose=false;

		public string _mStatus = ActionMyStatus.Add;

        private const string MSG_NO_DEBIT = "Không còn nợ";
		private CustomerService _customerService;
		private string strCusID = string.Empty;

        #endregion

        #region Method
        void Save()
        {

			_customerService = new CustomerService();
            

            //Update
			//Add
			if (_mStatus == ActionMyStatus.Edit)
			{
				DataSet dsCustomer = _customerService.Its_Water_Customer_Update(
																strCusID
															, this.GetTextBoxStringValue(txtCusCode)
															, this.GetTextBoxStringValue(txtCusName)
															, this.GetTextBoxStringValue(txtOrgName)
															, txtAddress.Text.Trim()
															, txtTel.Text.Trim()
															, this.GetTextBoxInt32Value(txtDebit).ToString()
															, txtNote.Text
															, this.GetTextBoxInt32Value(txtNovoCurrent).ToString()
															, this.GetTextBoxInt32Value(txtCuocVo).ToString()
															);
			}

			//Add
			if (_mStatus == ActionMyStatus.Add)
			{
				DataSet dsCustomer = _customerService.Its_Water_Customer_Create(
															this.GetTextBoxStringValue(txtCusCode)
															, this.GetTextBoxStringValue(txtCusName)
															, this.GetTextBoxStringValue(txtOrgName)
															, txtAddress.Text.Trim()
															, txtTel.Text.Trim()
															, this.GetTextBoxInt32Value(txtDebit).ToString()
															, txtNote.Text
															, this.GetTextBoxInt32Value(txtNovoCurrent).ToString()
															, this.GetTextBoxInt32Value(txtCuocVo).ToString()
															);
				this.strCusID = dsCustomer.Tables[DbTable.Tbl_Customer].Rows[0][TblCustomer.CusID].ToString();
				this._mStatus = ActionMyStatus.Edit;
			}

			if (_sendValue != null)
			{
				_sendValue(
							strCusID
							, txtAddress.Text
							, txtCusName.Text
							, txtTel.Text
							, txtOrgName.Text
							, this.GetTextBoxInt32Value(txtDebit).ToString()
							);
			}
			if (isclose)
			{
				this.Close();
			}
        }

        bool CheckInPut()
        {
            
         
            if (StringUtils.IsEmpty(txtCusCode.Text))
            {
                Util.ShowWarningMsgBox("Nhập Mã khách hàng.");
                txtCusCode.Focus();
                return false;
            }        


          if (txtOrgName.Text == "")
          {
              Util.ShowWarningMsgBox("Nhập tên tổ chức Khách hàng");
              txtOrgName.Focus();
              return false;
          }

            if (txtCusName.Text == "")
            {
                Util.ShowWarningMsgBox("Nhập tên tổ chức Khách hàng liên hệ");
                txtCusName.Focus();
                return false;
            }

            if (txtAddress.Text == "")
            {
                Util.ShowWarningMsgBox("Nhập địa chỉ Khách hàng");
                txtAddress.Focus();
                return false;
            }

            if (StringUtils.IsEmpty(txtDebit.Text))
            {
                Util.ShowWarningMsgBox("Nhập công nợ khách hàng");
                txtDebit.Focus();
                return false;
            }

            return true;
        }

        void BindDataToControl(string _strCusID, string _strAddress, string _strCusCode, string _strCusName, string _strNote
								, string _strOrgName, string _strCusTel, string _strDebit, string _strNoVoCurrent, string _strCuocVoCurrent)
        {
			this.strCusID = _strCusID;
			txtAddress.Text = _strAddress;
			txtCusCode.Text = _strCusCode;
			txtCusName.Text = _strCusName;
			txtNote.Text = _strNote;
			txtOrgName.Text = _strOrgName;
			txtTel.Text = _strCusTel;
			txtDebit.EditValue = _strDebit;
			txtNovoCurrent.EditValue = _strNoVoCurrent;
			txtCuocVo.EditValue = _strCuocVoCurrent;
        }

        void CleanData()
        {
            txtDebit.EditValue = 0;
            txtAddress.Text = "";
            txtCusCode.Text = "";
            txtCusName.Text = "";
            txtNote.Text = "";
            txtOrgName.Text = "";
            txtTel.Text = "";
        }
        #endregion 

        #region Event

        public FrmCustomer()
        {
            InitializeComponent();

            this.IsExit = false;
            this.SetEventFormClosing();
        }

        public FrmCustomer(bool IsLoadFromBooking)
        {
            InitializeComponent();
            this.isclose = IsLoadFromBooking;
			this.IsExit = false;
            this.SetEventFormClosing();
        }

		public FrmCustomer(string _strCusID, string _strAddress, string _strCusCode, string _strCusName, string _strNote
								, string _strOrgName, string _strCusTel, string _strDebit, string _strNoVoCurrent, string _strCuocVoCurrent)
		{
			InitializeComponent();
			this.IsExit = false;
			this.SetEventFormClosing();
			this._mStatus = ActionMyStatus.Edit;
			BindDataToControl(
								_strCusID
								, _strAddress
								, _strCusCode
								, _strCusName
								, _strNote
								, _strOrgName
								, _strCusTel
								, _strDebit
								, _strNoVoCurrent
								, _strCuocVoCurrent);
		}

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckInPut())
                {
                    Save();
					this.IsExit = false;
                    Util.ShowMessageBox("Đã lưu thông tin khách hàng");
                }
            }
            catch (Exception ex)
            {
                Util.ShowExceptionBox(ex);
            }
        }

        private void FrmCustomer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Control)
            {
                try
                {
                    if (CheckInPut())
                    {
                        Save();
                        Util.ShowMessageBox("Đã lưu thông tin khách hàng");
                    }
                }
                catch (Exception ex)
                {
                    Util.ShowExceptionBox(ex);
                }
            }
        }

        private void FrmCustomer_Load(object sender, EventArgs e)
        {
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (Util.ShowMsgBoxWarningQuestion("Bạn muốn xóa thông tin khách hàng???"))
                {
					_customerService = new CustomerService();
					_customerService.Its_Water_Customer_Delete(strCusID);
                    CleanData();
                }
            }
            catch (Exception ex)
            {
                Util.ShowExceptionBox(ex);
            }
        }        

        private void btnToPay_Click(object sender, EventArgs e)
        {
            if (Convert.ToDouble(txtDebit.EditValue)<=0)
            {
                Util.ShowWarningMsgBox(MSG_NO_DEBIT);
                return;
            }
			Water.FrmWaterReceipt frm = new FrmWaterReceipt(strCusID, txtCusName.Text, txtDebit.Text.Trim());
			//frm._waterCustomer = _customer;
			//frm._sendValue = new FrmWaterReceipt.SendValue(GetDebit);
			frm.ShowDialog();
        }

        void GetDebit(double _debit)
        {
            txtDebit.EditValue = _debit;
        }

        private void btnTravo_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txtNovoCurrent.EditValue) <= 0)
            {
                Util.ShowWarningMsgBox(MSG_NO_DEBIT);
                return;
            }
			FrmWaterPayCover frm = new FrmWaterPayCover(strCusID, txtCusName.Text, txtNovoCurrent.Text.Trim());
			//frm._waterCustomer = _customer;
			//frm._sendValue = new FrmWaterPayCover.SendValue(GetNoVoCurrent);
			frm.ShowDialog();
        }

		private void btnCuocVo_Click(object sender, EventArgs e)
		{
			if (Convert.ToInt32(txtCuocVo.EditValue) <= 0)
			{
				Util.ShowWarningMsgBox(MSG_NO_DEBIT);
				return;
			}
			FrmWaterPayCuocVo frm = new FrmWaterPayCuocVo(strCusID, txtCusName.Text, txtCuocVo.Text.Trim());
			//frm._waterCustomer = _customer;
			//frm._sendValue = new FrmWaterPayCuocVo.SendValue(GetNoVoCurrent);
			frm.ShowDialog();
		}

        void GetNoVoCurrent(int  _noVo)
        {
            txtNovoCurrent.EditValue = _noVo;
        }

        #endregion

        private void txtCusName_Modified(object sender, EventArgs e)
        {
			this.IsExit = true;
        }		
    }
}
